package com.example.testapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.example.testapplication.util.DensityUtil;

import java.util.ArrayList;

/**
 * 自定义下拉框
 *
 * */
public class CustomWindon extends AppCompatActivity {
    private EditText et_input;
    private ImageView iv_down_arrow;

    private PopupWindow popupWindow;
    private ListView listView;
    private MyAdapter myAdapter;

    private ArrayList<String> msgs;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_custom_windon);

        iv_down_arrow = (ImageView) findViewById(R.id.iv_down_arrow);
        et_input = (EditText) findViewById(R.id.et_input);

        et_input.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(popupWindow == null){
                    // 创建弹框
                    popupWindow = new PopupWindow(CustomWindon.this);
                    popupWindow.setWidth(et_input.getWidth());
                    // dp - px
                    int height = DensityUtil.dip2px(CustomWindon.this,200);
                    Toast.makeText(CustomWindon.this, "height="+ height, Toast.LENGTH_SHORT).show();
                    popupWindow.setHeight(height);
                    //调用setContentView(int id) 加载istView
                    popupWindow.setContentView(listView);
                    popupWindow.setFocusable(true); // 设置焦点

                }
                // showAsDropDown（控件）:弹出的位置和 控件的位置关系如下
                popupWindow.showAsDropDown(et_input,0,0);
            }
        });

        listView = new ListView(this);
        listView.setBackgroundResource(R.drawable.listview_background);

        // 模拟数据
        msgs = new ArrayList<>();
        for(int i = 0; i < 100;i++){
            msgs.add(i+ "----aaaaaaaaaa-----" +i);
        }

        myAdapter = new MyAdapter();
        listView.setAdapter(myAdapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                // 1.得到数据
                String msg = msgs.get(i);
                //2. 设置输入框
                et_input.setText(msg);

                if(popupWindow != null && popupWindow.isShowing()){
                    popupWindow.dismiss();
                    popupWindow = null;
                }
            }
        });

    }
    class MyAdapter extends BaseAdapter{

        @Override
        public int getCount() {
            return msgs.size();
        }

        @Override
        public Object getItem(int i) {
            return null;
        }

        @Override
        public long getItemId(int i) {
            return 0;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            ViewHolder viewHolder;
            if(view == null){
                view = View.inflate(CustomWindon.this,R.layout.pop_item,null);
                viewHolder = new ViewHolder();
                viewHolder.tv_msg = (TextView) view.findViewById(R.id.tv_msg);
                viewHolder.iv_delete = (ImageView) view.findViewById(R.id.iv_delete);
                view.setTag(viewHolder);
            }else{
                viewHolder = (ViewHolder) view.getTag();
            }

            // 根据位置得到数据
            final String msg = msgs.get(i);
            viewHolder.tv_msg.setText(msg);

            // 设置删除
            viewHolder.iv_delete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    // 1.从集合中删除
                    msgs.remove(msg);
                    // 2.刷新ui -适配器刷新
                    myAdapter.notifyDataSetChanged();
                }
            });
            return view;
        }
    }

    static class ViewHolder{
        TextView tv_msg;
        ImageView iv_delete;
    }



}

